package com.fangcun.service.api.impl;

import java.util.ArrayList;
import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import com.fangcun.common.canstant.ErrCode;
import com.fangcun.common.canstant.PublicConstant;
import com.fangcun.common.exception.ProAffairException;
import com.fangcun.common.exception.ProRunTimeException;
import com.fangcun.entity.Agent;
import com.fangcun.entity.AgentGoods;
import com.fangcun.mapper.AgentGoodsMapper;
import com.fangcun.mapper.AgentMapper;
import com.fangcun.service.api.AgentGoodsService;

/**
 * @作者： 陳曉鬆 @时间： 2020年8月16日 - 上午9:20:38 @description：->
 */
@Service(value = "agentGoodsService")
public class AgentGoodsServiceImpl implements AgentGoodsService {

	@Autowired
	private AgentGoodsMapper agentGoodsMapper;
	@Autowired
	private AgentMapper agentMapper;

	@Override
	@Transactional
	public void agentChainChange(List<AgentGoods> goods, Long userId) throws ProAffairException {
		if (goods == null || goods.size() == 0 || goods.get(0).getAgentId() == null) {
			throw new ProRunTimeException(ErrCode.ERR_PARAMETER.getReturnCode(), ErrCode.ERR_PARAMETER.getReturnMsg());
		}
		// 查询是否是本代理商
		Agent agentClass = agentMapper.selectByPrimaryKey(goods.get(0).getAgentId());
		if (agentClass == null || agentClass.getUserId().longValue() != userId.longValue()) {
			throw new ProRunTimeException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		List<AgentGoods> updateGoods = new ArrayList<AgentGoods>();
		for (AgentGoods g : goods) {
			if (g.getAgentId() == null || g.getGoodsId() == null) {
				throw new ProRunTimeException(ErrCode.ERR_PARAMETER.getReturnCode(), ErrCode.ERR_PARAMETER.getReturnMsg());
			}
			try {
				agentGoodsMapper.insertSelective(g);
			} catch (DuplicateKeyException e) {
				g.setStatus(PublicConstant.GOODS_STATUS_1);
				updateGoods.add(g);
			}
		}
		if (updateGoods != null && updateGoods.size() > 0) {
			agentGoodsMapper.updateForStatus(updateGoods);
		}
	}

	@Override
	@Transactional
	public void agentCancelChainChange(List<AgentGoods> goods, Long userId) throws ProAffairException {
		if (goods == null || goods.size() == 0 || goods.get(0).getAgentId() == null) {
			throw new ProRunTimeException(ErrCode.ERR_PARAMETER.getReturnCode(), ErrCode.ERR_PARAMETER.getReturnMsg());
		}
		// 查询是否是本代理商
		Agent agentClass = agentMapper.selectByPrimaryKey(goods.get(0).getAgentId());
		if (agentClass == null || agentClass.getUserId().longValue() != userId.longValue()) {
			throw new ProRunTimeException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		List<Agent> agent = agentMapper.selectByParentId(goods.get(0).getAgentId());
		List<AgentGoods> updateGoods = new ArrayList<AgentGoods>();
		List<AgentGoods> updateChildGoods = null;
		if (agent != null && agent.size() > 0) {
			updateChildGoods = new ArrayList<AgentGoods>();
		}
		for (AgentGoods g : goods) {
			if (g.getStatus() == null) {
				g.setStatus(PublicConstant.GOODS_STATUS_0);
			}
			updateGoods.add(g);
			if (g.getStatus().intValue() == PublicConstant.GOODS_STATUS_2 && agent != null && agent.size() > 0) {
				for (Agent a : agent) {
					g.setAgentId(a.getId());
					updateChildGoods.add(g);
				}
			}
		}
		agentGoodsMapper.updateForStatus(updateGoods);
		if (updateChildGoods != null && updateChildGoods.size() > 0) {
			agentGoodsMapper.updateForStatus(updateChildGoods);
		}
	}

}
